//
//  RTJLoginController.m
//  RuiTaiJinProject
//
//  Created by jayson on 2018/11/27.
//  Copyright © 2018年 leewei. All rights reserved.
//

#import "RTJLoginController.h"
#import "RTJForgetPassWordController.h"
#import "UIButton+Category.h"


@interface RTJLoginController ()<UITextFieldDelegate,ZXRegTextFieldDelegate>

@property (weak, nonatomic) IBOutlet UIButton *sendSMSBtn;
@property (weak, nonatomic) IBOutlet UIButton *loginBtn;
@property (weak, nonatomic) IBOutlet ZXRegTextField *pwTxt;
@property (weak, nonatomic) IBOutlet UILabel *phoneLab;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *viewTop;
@end

@implementation RTJLoginController

- (void)viewDidLoad {
    [super viewDidLoad];
    
     self.viewTop.constant = screenH == 812 ? 110 : 90;
    self.title = _isSMS ? @"短信登陆" : @"登陆";
    _phoneLab.text = _phoneStr;
    if (_isSMS) {
        
        [_sendSMSBtn startWithTime: 60 title:@"重新获取验证码" countDownTitle:@"s" mainColor:[UIColor whiteColor] countColor:[UIColor whiteColor]];
        [_sendSMSBtn setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
        
    }else{
        
        [_sendSMSBtn setImage:[UIImage imageNamed:@"icon_yanjin"] forState:UIControlStateNormal];
        
    }
    
    _pwTxt.delegate = self;
    _pwTxt.clearsOnBeginEditing = NO;
    _pwTxt.zxdelegate = self;
    _pwTxt.zxType = ZXRegTextFieldTypeChars;
    _pwTxt.secureTextEntry = YES;
    _loginBtn.userInteractionEnabled = NO;
    [_loginBtn setBackgroundImage:[UIImage imageNamed:@"btn_qrcjhui"] forState: UIControlStateNormal];

}

-(void)textFieldDidEndEditing:(UITextField *)textField{
    
    _loginBtn.userInteractionEnabled = textField.text.length!=0 ? YES : NO;
    [_loginBtn setBackgroundImage:textField.text.length!=0 ?[UIImage imageNamed:@"btn_ljcj"]:[UIImage imageNamed:@"btn_qrcjhui"] forState: UIControlStateNormal];
    
}


- (IBAction)sendSMS:(UIButton *)sender {
    
    if (!_isSMS) {
        
        sender.selected = !sender.isSelected;
        
        _pwTxt.secureTextEntry = !sender.selected;
        
    }else{
        
        

    }
    
}


- (IBAction)forgetPassWord:(id)sender {
    
    RTJForgetPassWordController *vc = [[RTJForgetPassWordController alloc]init];
    vc.title = @"找回密码";
    vc.isPayPassWord = NO;
    vc.isForgetPassWord = YES;
    vc.phoneStr = _phoneStr;
    [self.navigationController pushViewController:vc animated:YES];
    
}

- (IBAction)SMSLogin:(id)sender {
    
    [SVProgressHUD showErrorWithStatus:@"正在开发中，敬请期待"];
//    RTJLoginController *loginVC = [[RTJLoginController alloc]init];
//    loginVC.isSMS = YES;
//    [self.navigationController pushViewController:loginVC animated:YES];
    
}

- (IBAction)login:(UIButton *)sender {
    
    [SVProgressHUD showWithStatus:nil];
    if ( _pwTxt.text.length == 0) {
        
        [SVProgressHUD showErrorWithStatus:@"请输入密码"];
        return;
    }

    NSDictionary *dic =@{@"user_name":_phoneStr,
                         @"user_pwd":_pwTxt.text
                         };
    [RTJNetWork asyncRequestWithURL:@"user/login" params:dic method:POST zxCompletion:^(id data, NSInteger status, BOOL success, NSString *errorMsg) {
        
        [SVProgressHUD dismiss];
        
        if (success) {
            
            if ([data[@"status"]integerValue] == 1) {
                
                [RTJUserTool saveTheUserInfo:data[@"data"]];
                
                [self.navigationController popToRootViewControllerAnimated:NO];
                
            }else{
                
                [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"登录失败:%@",data[@"msg"]]];
            }
            
        }else{
            
            [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"登录失败:%@",errorMsg]];

        }
        
    }];

}


@end
